//
//  PopoverAnimate.swift
//  KKWB
//
//  Created by KSY-iOS on 17/1/10.
//  Copyright © 2017年 KK. All rights reserved.
//

import UIKit

class PopoverAnimate: NSObject {

    fileprivate var isPresented: Bool = false
    
    var callBack: ((_ presented: Bool) -> ())?
    
    override init() {
        
    }
    
    init(callBack: (_ presented: Bool) -> ()) {
        
        
    }
    
    
}

extension PopoverAnimate: UIViewControllerTransitioningDelegate {
    
    // 系统回调函数 默认internal修饰符
    internal func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        return PopPresentationController(presentedViewController: presented, presenting: presenting)
    }
    // 自定义弹出动画
    internal func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        callBack?(isPresented)
        
        return self
    }
    // 自定义消失动画
    internal func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        
        callBack?(isPresented)
        
        return self
    }
    
}

extension PopoverAnimate: UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animateForPresented(using: transitionContext) : animateForDismessed(using: transitionContext)
    }
    
    fileprivate func animateForPresented(using transitionContext: UIViewControllerContextTransitioning) {
        
        let presentView = transitionContext.view(forKey: .to)!
        
        transitionContext.containerView.addSubview(presentView)
        
        presentView.transform = CGAffineTransform(scaleX: 1.0, y: 0)
        // 设置锚点
        presentView.layer.anchorPoint = CGPoint(x: 0.5, y: 0);
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            presentView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        }) { (_) in
            // 告诉上下文 你已经完成动画
            transitionContext.completeTransition(true)
        }
        
    }
    
    fileprivate func animateForDismessed(using transitionContext: UIViewControllerContextTransitioning) {
        
        let dissmissView = transitionContext.view(forKey: .from)!
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            dissmissView.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
        }) { (_) in
            
            dissmissView.removeFromSuperview()
            // 告诉上下文 你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
}
